﻿import QtQuick 2.0

import "./components"
import "./pageComponents"
import "./animation"
import "./chromatogram"
import "./promptBox"

Item {
    id: root
    height: parent.height; width: parent.width

    property int animShowMode: 0   // 0正常 1向左扩大
    property int animFullScreen: 0 // 0不全屏 1全屏


    property var logInfo: [{
            "FinishTime": "0",
            "Time": "0",
            "Step": "0",
            "Status": "进行中",
            "ActionInfo": "1"
        }]


    property bool mutexExpInfo: false
    onVisibleChanged: {
        if(visible){
            state2Edit()
        }else{
            gStateExp.isConfirm_4 = false;
        }
    }

    function state2Edit(){
        combo_mainMethod.setText(gStateExp.mainMethod)
        combo_singleMethod.setText(gStateExp.singleMethod_4)
        combo_consumableName.setText(gStateExp.consumableName)
        edit_code.setText(gStateExp.consumableCode)
        updateExpModel()
        updateLogInfo()
        refreshConsumbaleCodeState()
        edit_expName.setText(gStateExp.expName)
    }
    function refreshConsumbaleCodeState(){
        gStateExp.checkConsumableCode(edit_code.getText())
        edit_code.redBorder = edit_code.getText() === "" ? false : !gStateExp.consumableCodeStatus
        edit_code.redInfo = edit_code.getText() === "" ? "" : gStateExp.consumableCodePromptMsg
    }


    // 更新当前页面的实验日志
    function updateLogInfo(){
        var logInfo
        if(gExpIsRunning === 1){       // 运行中/暂停
            logInfo = CGetMessageInterface.getExpLog(4)
        }else if(gExpIsRunning === 0){     // 空闲/复位
            let module = gStateExp.mainMethod === "" ? 1 : 0
            let name = gStateExp.mainMethod === "" ? gStateExp.singleMethod_4 : gStateExp.mainMethod

            // 主流程
            if(gStateExp.mainMethod !== ""){
                CGetMessageInterface.refMainExpLog(gStateExp.mainMethod)    // 通知后端刷新日志
            }
            logInfo = CGetMessageInterface.getExperimentLog(4, gStateExp.mainMethod === "" ? 4 : 0, gStateExp.singleMethod_4)
        }
        expLog.setData(logInfo)

        // 步骤数
        if(gStateExp.mainMethod === ""){
            gStateExp.expStepCount = CGetMessageInterface.getExperimentStepCount(4)
        }else{
            gStateExp.expStepCount = CGetMessageInterface.getExperimentStepCount(0)
        }
    }


    // 更新实验名称的model
    function updateExpModel(){

        // 主流程Model
        var currentModel = combo_mainMethod.getText()
        combo_mainMethod.model = CGetMessageInterface.getMainMethodList()
        combo_mainMethod.setIndex(combo_mainMethod.model.indexOf(currentModel))

        // 耗材Model
        currentModel = combo_consumableName.getText()
        combo_consumableName.model = CGetMessageInterface.getConsumableList()
        combo_consumableName.setIndex(combo_consumableName.model.indexOf(currentModel))
        // 单个流程Model
        currentModel = combo_singleMethod.getText()
        combo_singleMethod.model = CGetMessageInterface.getSingleMethodList(4, gStateExp.consumableName);
        if(combo_mainMethod.getText() === ""){      // 主流程为空时，显示列表
            combo_singleMethod.setIndex(combo_singleMethod.model.indexOf(currentModel))
        }else{                                      // 主流程不为空时，和主流程的设置同步
            combo_singleMethod.setText(CGetMessageInterface.getSingleMethodByMainMethod(combo_mainMethod.getText())[3])
        }

    }


    ControlPannel{
        id: controlPannel
    }
    // 左边的动画
    AnimationLayout4{
        id: layout_animation
        y: {
            if(animFullScreen === 1)     return 0
            else                         return controlPannel.y + controlPannel.height
        }
        height: root.height - y
        width: {
            if(animShowMode === 0 && animFullScreen === 0)  return root.width - layout_info.width - 10*dp
            else                              return root.width
        }
        onFullScreen: {
            animFullScreen = 1 - animFullScreen
        }
        CustomImage{
            visible: animFullScreen !== 1     // 全屏时不显示
            width: 12*dp;  height: 82*dp
            anchors.verticalCenter: parent.verticalCenter
            source: animShowMode===0 ? "qrc:/picture/animation/leftExpand.png" : "qrc:/picture/animation/leftShrink.png"
            x: parent.width - width
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape: "PointingHandCursor"
                onEntered: { parent.opacity = 0.8 }
                onExited: { parent.opacity = 1 }
                onClicked: {
                    animShowMode = 1 - animShowMode
                }
            }
        }
        z: 100
    }
    // 右边的实验信息
    Item{
        id: layout_info
        x: layout_animation.x + layout_animation.width + 10*dp
        y: controlPannel.y + controlPannel.height
        height: root.height - y;  width: 610*dp


        property real titleWidth: gAppModule===2 ? 100*dp : 115*dp
        property real editWidth: gAppModule===2 ? 470*dp : 440*dp

        // 实验信息
        Rectangle{
            id: layout_expInfo
            height: showDetailsInfo ? 538*dp : 300*dp
            width: parent.width
            radius: 5*dp
            property bool showDetailsInfo: false


            // 下拉动画效果
            state: showDetailsInfo ? "show" : "hide"
            states: [
                State{
                    name: "show"
                    PropertyChanges {
                        target: layout_expInfo
                        height: 546*dp
                    }
                },
                State{
                    name: "hide"
                    PropertyChanges {
                        target: layout_expInfo
                        height: 300*dp
                    }
                }
            ]
            transitions: [
                Transition {
                    from: "show"
                    to: "hide"
                    NumberAnimation{
                        properties: "height"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                },Transition {
                    from: "hide"
                    to: "show"
                    NumberAnimation{
                        properties: "height"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                }
            ]

            Item{
                height: 30*dp; width: parent.width - 2*x
                x: 22*dp; y: 20*dp
                Text{
                    text: "实验信息"
                    font.bold: true
                    font.pixelSize: config.fontsize_1
                }
            }

            Column{
                id: column_expInfo
                x: 22*dp; y: 60*dp
                height: 40; width: parent.width
                spacing: 15*dp
                CustomComboBox{
                    id: combo_mainMethod
                    title: "项目名称"
                    visible: gAppModule === 0               // 屏蔽模块
                    showClearButton: true

                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth
                    enabled: gExpIsRunning === 0
                    onClickText: {
                        gStateExp.mainMethod = text
                        if(index===-1){         // 清空
                            gStateExp.clearExpSelected()
                        }
                        state2Edit()
                    }
                }
                CustomInputText{
                    id: edit_expName
                    title: "名称"
                    visible: gAppModule === 2

                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth
                    enabled: gExpIsRunning === 0
                    onTextChanged: {
                        gStateExp.expName = getText()
                    }
                }
                CustomComboBox{
                    id: combo_consumableName
                    enabled: combo_mainMethod.text === "" && gExpIsRunning === 0
                    title: "耗材名称"

                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth - 130*dp
                    onClickText: {
                        gStateExp.consumableName = text
                        gStateExp.singleMethod_4 = ""
                        state2Edit()
                    }
                    CustomButton{
                        id: button_confirm
                        title: "更换纯化耗材"
                        enabled: gExpIsRunning === 0
                        anchors.verticalCenter: parent.verticalCenter
                        defaultMode1: true
                        x: parent.width + 10*dp
                        onClicked: {
    //                        gStateExp.confirmExp()
                            gStateExp.confirmConsumableName1()
                        }
                    }
                }CustomComboBox{
                    id: combo_singleMethod
                    enabled: combo_mainMethod.text === "" && combo_consumableName.text!=="" && gExpIsRunning === 0
                    title: gAppModule===0?"模块实验名称":"实验名称"

                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth
                    editDefaultText: enabled ? "" : "请先选择耗材名称"
                    onClickText: {
                        gStateExp.singleMethod_4 = text
                        state2Edit()
                    }
                }CustomInputText{
                    id: edit_code
                    editable: gExpIsRunning === 0
                    title: "耗材编码"

                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth
                    onTextChanged: {
                        gStateExp.consumableCode = getText()

                        // 判断是否有不合法字符
                        var res = true
                        for(let i = 0; i < getText().length; i ++){
                            console.log(config.validChar.indexOf(getText()[i])<0)
                            if(config.validChar.indexOf(getText()[i])<0){
                                res = false
                                break
                            }
                        }
                        console.log(res)
                        if(!res){
                            redBorder = true
                            redInfo = "只能输入英文、数字、下划线"
                        }else{
                            redBorder = false
                        }
                    }
                    onEnterPressed: {
                        refreshConsumbaleCodeState()
                    }onEditingFinished: {
                        refreshConsumbaleCodeState()
                    }
                    inputMaxLength: 20
                    Text{
                        text: gStateExp.consumableCodeStatus ? "有效" : "无效"
                        color: gStateExp.consumableCodeStatus ? "green" : "red"
                        height: 28*dp; width: 48*dp
                        verticalAlignment: Text.AlignVCenter;  horizontalAlignment: Text.AlignHCenter
                        anchors.verticalCenter: parent.verticalCenter
                        x: parent.width - width - 5*dp
                        font.pixelSize: config.fontsize
                        MouseArea{
                            cursorShape: "PointingHandCursor"
                            anchors.fill: parent
                            onClicked: {
                            }
                        }
                    }
                }
                CustomInputText{
                    id: edit_equName
                    editable: gExpIsRunning === 0
                    visible: gAppModule === 2 && layout_expInfo.showDetailsInfo
                    title: "设备名称"
                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth

                    onTextChanged: {
                        gStateExp.equName = getText()
                        state2Edit()
                    }
                }
                CustomComboCalendarGrid{
                    id: edit_date
                    enabled: gExpIsRunning === 0
                    visible: gAppModule === 2 && layout_expInfo.showDetailsInfo
                    title: "实验日期"
                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth

                    onDateChanged: {
                        gStateExp.execDate = getText()
                        state2Edit()
                    }
                }
                CustomInputText{
                    id: edit_operator
                    editable: gExpIsRunning === 0
                    visible: gAppModule === 2 && layout_expInfo.showDetailsInfo
                    title: "操作人"
                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth

                    onTextChanged: {
                        gStateExp.operator = getText()
                        state2Edit()
                    }
                }
                CustomInputText{
                    id: edit_memo
                    editable: gExpIsRunning === 0
                    visible: gAppModule === 2 && layout_expInfo.showDetailsInfo
                    title: "描述"
                    titleWidth: layout_info.titleWidth
                    editWidth: layout_info.editWidth
                    editHeight: 60*dp
                    editWrapModel: 1

                    onTextChanged: {
                        gStateExp.consumableCode = getText()
                    }
                }


            }
            Rectangle{
                visible: gAppModule === 2
                anchors.horizontalCenter: parent.horizontalCenter
                y: parent.height - 26*dp
                height: 20*dp; width: 130*dp
                Text {
                    text: layout_expInfo.showDetailsInfo ? "收起" : "展开查看更多"
                    font.pixelSize: config.fontsize
                    color: "#1677FF"
                    anchors.centerIn: parent
                    anchors.horizontalCenterOffset: -10*dp
                    Image {
                        height: 24*dp; width: 24*dp
                        anchors.verticalCenter: parent.verticalAlignment
                        x: parent.width
                        source: layout_expInfo.showDetailsInfo ? "qrc:/picture/uparrow_b.png" : "qrc:/picture/downarrow_b.png"
                    }
                }
                MouseArea{
                    anchors.fill: parent
                    cursorShape: Qt.PointingHandCursor
                    onClicked: {
                        layout_expInfo.showDetailsInfo = !layout_expInfo.showDetailsInfo
                    }
                    hoverEnabled: config.hoverEnabled
                    onEntered: {
                        parent.color = "#08111111"
                    }onExited: {
                        parent.color = "white"
                    }
                }
            }

        }
//        Chromatogram{
//            id: chromatogram
//            Text{
//                x: 20*dp; y: 20*dp
//                text: "能谱图"
//                font.bold: true
//                font.pixelSize: config.fontsize_1
//            }
//            y: layout_expInfo.y + layout_expInfo.height + 10*dp
//            height: 300*dp;     width: parent.width
//        }

        // 能谱图
//        Rectangle{
//            id: layout_chrom
//            y: layout_expInfo.y + layout_expInfo.height + 10*dp
//            height: 300*dp;     width: parent.width
//            color: "#E9E6E0"
//            Item{
//                height: 30*dp; width: parent.width - 2*x
//                x: 20*dp; y: 20*dp
//                Text{
//                    text: "能谱图"
//                    font.bold: true
//                    font.pixelSize: config.fontsize_1
//                }CustomButton{
//                    id: button_auto
//                    title: "自动"
//                    width: 88*dp; height: 32*dp
//                    anchors.verticalCenter: parent.verticalCenter
//                    defaultMode2: true
//                    x: button_confirm.x - width - 10*dp
//                    onClicked: {
//                        if(title === "自动"){
//                            title = "手动"
//                        }else{
//                            title = "自动"
//                        }
//                    }
//                }CustomButton{
//                    id: button_export
//                    title: "导出"
//                    width: 88*dp; height: 32*dp
//                    anchors.verticalCenter: parent.verticalCenter
//                    defaultMode1: true
//                    x: parent.width - width
//                    onClicked: {
//                    }
//                }
//            }CustomImage{
//                y: 65*dp; height: parent.height - y - 16*dp
//                x: 20*dp; width: parent.width - 2*x
//                source: "qrc:/picture/chromatogram.png"
//            }
//        }

        // 实验日志
        Rectangle{
            id: layout_expLog
            y: layout_expInfo.y + layout_expInfo.height + 10*dp
            height: parent.height - y; width: parent.width
            radius: 5*dp
            ExperimentLog{
                id: expLog
            }
        }

    }

    BoxRuDongBeng{          // 英文太长了我背不下来了
        id: box_rudongbeng
    }

}
